﻿

using System.Windows.Media.Imaging;
using System.IO;
using System;
namespace GestionAutoEcoleCommon.Util
{
    public class ImageConverter
    {
        public static byte[] imageToByteArray(BitmapImage imageIn)
        {
            byte[] result = null;

            if (imageIn != null)
            {
                using (MemoryStream memStream = new MemoryStream())
                {
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(imageIn));
                    encoder.Save(memStream);

                    result = memStream.ToArray();
                }

            }

            return result;
        }

        public static BitmapImage byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn != null && byteArrayIn.Length > 0)
            {
                var ms = new MemoryStream(byteArrayIn);

                var bitmapImg = new BitmapImage();

                bitmapImg.BeginInit();
                bitmapImg.StreamSource = ms;
                bitmapImg.EndInit();

                return bitmapImg;
            }
            return null;

        }
    }
}
